package com.ls.lserp.modules.material.service;


import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.dao.SlopworkInboundDao;
import com.ls.lserp.modules.material.entity.SlopworkInbound;
import com.ls.lserp.modules.order.dao.BigcargoOrderDao;
import com.ls.lserp.modules.order.entity.BigcargoOrder;
import com.ls.lserp.modules.produce.dao.ProduceLicenseDao;
import com.ls.lserp.modules.produce.entity.ProduceLicense;

/**
 * @ClassName: SlopworkInRequestService
 * @Description: 物料入库申请
 * 
 * @author 
 * @date 2017年04月14日
 *
 */

@Service
@Transactional(readOnly = false)
public class SlopworkInboundService extends CrudService<SlopworkInboundDao,SlopworkInbound>{

    @Autowired
    private SlopworkInboundDao slopworkInboundDao;
    //大货订单
  	@Autowired
    private BigcargoOrderDao bigcargoOrderDao;
  	//生产制单
  	@Autowired
    private ProduceLicenseDao produceLicenseDao;
    
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<SlopworkInbound> findList(Page<SlopworkInbound> page, SlopworkInbound slopworkInbound) {
		List<SlopworkInbound> irDetailList = null;
		BigDecimal tempRealQuantity = null;
		
		// tab页分，检索条件设定(0:全部,1:待入库,2:未全部入库,3:全部入库)
		slopworkInbound.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
		
		// 入出库区分（入库)
		slopworkInbound.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
		
		// 设置分页参数
		slopworkInbound.setPage(page);
		List<SlopworkInbound> resultList = slopworkInboundDao.findList(slopworkInbound);
		
		if(!CollectionsUtils.isEmpty(resultList)){
			// 库存申请信息
			for(SlopworkInbound ir : resultList){
				tempRealQuantity = new BigDecimal("0");
				irDetailList = slopworkInboundDao.findRealQuantityList(ir);
				if(!CollectionsUtils.isEmpty(irDetailList)){
					// 库存申请明细
					for(SlopworkInbound ird : irDetailList){
						tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealQuantity()));
					}
					
					// 实际入库数量
					ir.setRealQuantity(tempRealQuantity);
					
					// 金额(单价*实际入库数量)
					ir.setTotalAmount(tempRealQuantity.multiply(checkBigDecimalNull(ir.getIndentPrice())).setScale(2, BigDecimal.ROUND_HALF_UP));
				}
			}
		}
		
        // 执行分页查询
        page.setList(resultList);
        
        return page;
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
	/**
	 * @Title: findShowRequestDetailList
	 * @Description: 入库申请明细
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return List<SlopworkInRequest>
	 * @throws
	 */
	public List<SlopworkInbound> findShowRequestDetailList(SlopworkInbound slopworkInbound) {
	   
	   return slopworkInboundDao.findShowRequestDetailList(slopworkInbound);
	}
	
	/**
	 * @Title: getClothesRequest
	 * @Description: 入库申请明细
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return SlopworkInRequest
	 * @throws
	 */
	public SlopworkInbound getClothesRequest(SlopworkInbound slopworkInbound) {
	   
	   return slopworkInboundDao.getClothesRequest(slopworkInbound);
	}
	
	/**
	 * @Title: findClothesInventoryDetails
	 * @Description: 成衣库存明细
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return List<SlopworkInRequest>
	 * @throws
	 */
	public List<SlopworkInbound> findClothesInventoryDetails(SlopworkInbound slopworkInbound) {
	   return slopworkInboundDao.findClothesInventoryDetails(slopworkInbound);
	}
	
   /**
	  * @Title: findRequestDetailsList
	  * @Description: 仓库集
	  * @param @return
	  * @return List<MaterialOutInbound> 
	  * @throws
	  */
	public List<SlopworkInbound> findWarehouseList(){
		return slopworkInboundDao.findWarehouseList();
	}
	
	/**
	  * @Title: findRackList
	  * @Description: 货架集
	  * @param @param slopworkInbound
	  * @param @return
	  * @return List<SlopworkInbound> 
	  * @throws
	  */
	public List<SlopworkInbound> findRackList(SlopworkInbound slopworkInbound){
		return slopworkInboundDao.findRackList(slopworkInbound);
	}
	
	/**
	   * @Title: saveSlopworkInbound
	   * @Description: 保存入库货架
	   * @param @param slopworkInbound
	   * @return void 
	   * @throws
	   */
	  @Transactional(readOnly = false)
	  public void saveSlopworkInbound(SlopworkInbound slopworkInbound) {
		  SlopworkInbound paramSlopwork = new SlopworkInbound();
		  slopworkInbound.preInsert();
		  slopworkInbound.setMaterialStatus(ResourcesUtil.getConfig("clothes_detail_status_in"));
		  slopworkInbound.setEnableFlag(SlopworkInbound.ENABlE_FLAG_NORMAL);
		  slopworkInbound.setDeleteFlag(SlopworkInbound.DEL_FLAG_NORMAL);
		  slopworkInbound.setVersion(1L);
		  if (slopworkInbound.getInventoryCategory().equals(ResourcesUtil.getConfig("clothes_in_category_Samply")))
		  {
			  slopworkInbound.setClothsType(ResourcesUtil.getConfig("cloths_type_samply"));
		  }else{
			  //
			  slopworkInbound.setClothsType(ResourcesUtil.getConfig("cloths_type_indent_slopwork"));
		  }
		  slopworkInboundDao.insertInventoryClothesDetail(slopworkInbound);
		  
		  // 判断成衣库存是否存在
		  SlopworkInbound swi = slopworkInboundDao.getInventoryClothes(slopworkInbound);
		  // 插入
		  if(swi==null){
			 // slopworkInbound.setClothsType(ResourcesUtil.getConfig("cloths_type_indent_slopwork"));
			  slopworkInboundDao.insertInventoryClothes(slopworkInbound);
		  // 编辑	  
		  }else{
			  swi.setClothsQuantity(checkBigDecimalNull(swi.getClothsQuantity()).add(checkBigDecimalNull(slopworkInbound.getRealQuantity())));
			  slopworkInboundDao.updateInventoryClothes(swi);
		  }
		  
		  BigDecimal tempRealQuantity = checkBigDecimalNull(slopworkInbound.getRequestRealQuantity()).add(checkBigDecimalNull(slopworkInbound.getRealQuantity()));
		  if(tempRealQuantity.compareTo(checkBigDecimalNull(slopworkInbound.getRequestQuantity()))==-1){
			  paramSlopwork.setStockinStatus(ResourcesUtil.getConfig("inventory_status_not_all"));
		  }else{
			  paramSlopwork.setStockinStatus(ResourcesUtil.getConfig("inventory_status_all"));
		  }
		  paramSlopwork.setInventoryRequestDetailId(slopworkInbound.getInventoryRequestDetailId());
		  paramSlopwork.setRequestRealQuantity(tempRealQuantity);
		  //更新实际入库数量和STOCKIN_STATUS 0-未入库 1-部分入库 2-全部入库
		  slopworkInboundDao.updateRequestDetailsRealQuantity(paramSlopwork);
		  //获取申请数量 入库数量
		  List<SlopworkInbound> rtnRequestQuantity = slopworkInboundDao.findRealQuantityList(slopworkInbound);
		  // 全部入库
		  boolean isAllInventory = false;
		  for(SlopworkInbound fs : rtnRequestQuantity){
			  if(checkBigDecimalNull(fs.getRealQuantity()).compareTo(checkBigDecimalNull(fs.getRequestQuantity()))==-1){
				  isAllInventory = true;
				  break;
			  }
		  }
		  
		  if(isAllInventory){
			  slopworkInbound.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_not_all"));
		  }else{
			  slopworkInbound.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_all"));
		  }
		  //更新INVENTORY_STATUS状态 0-待出库 1-部分入出库 2-全部入出库
		  slopworkInboundDao.updateRequestStatus(slopworkInbound);
		  //获取生产制单完成进度和生产状态
		  ProduceLicense produceLicense = produceLicenseDao.getProStatusInfo(slopworkInbound);
		  if (produceLicense != null && (int)(produceLicense.getDataRate()) == 100 && "0".equals(produceLicense.getProStatus())) {
			  // 更新生产制单生产状态为生产完成
			  produceLicense.preUpdate();
			  produceLicense.setProStatus("1");
			  produceLicenseDao.updateProduceStatus(produceLicense);
		  }
		  //获取大货订单完成进度和生产状态
		  BigcargoOrder bigcargoOrder = bigcargoOrderDao.getProStatusInfo(slopworkInbound);
		  if (bigcargoOrder != null && (int)(bigcargoOrder.getDataPercentage()) == 100 && "0".equals(bigcargoOrder.getProduceStatus())) {
			  // 更新大货订单生产状态为生产完成
			  bigcargoOrder.preUpdate();
			  bigcargoOrder.setProduceStatus("1");
			  bigcargoOrderDao.updateProduceStatus(bigcargoOrder);
		  }
		  
	  }
	/**
	 * @Title: findShowSamplyRequestDetailList
	 * @Description: 样品入库申请明细
	 * @param @param slopworkInRequest
	 * @param @return
	 * @return List<SlopworkInRequest>
	 * @throws
	 */
	public List<SlopworkInbound> findShowSamplyRequestDetailList(SlopworkInbound slopworkInbound) {
        slopworkInbound.setDeleteFlag("0");
		return slopworkInboundDao.findShowSamplyRequestDetailList(slopworkInbound);
	}
}